<?php

	include_once("include/config.php");
	include_once("include/auth.lib.php");
	include_once("include/functions.php");

	$filter_field = $_POST["qtype"];
	$filter_value = $_POST["query"];
	
	if ($filter_value != '') {
		$filter = "WHERE UPPER($filter_field) LIKE '%". strtoupper($filter_value) . "%'";
	} else {
		$filter = "WHERE 1 = 1";
	}
	

	$sql = "SELECT a.id, id_palestra, p.nome nome_palestra, cognomenome, categoria, peso 
			  FROM atleti a 
			  JOIN palestre p ON (a.id_palestra = p.id)
             $filter 			  
			 ORDER BY cognomenome";
	$result = runSQL($sql);
	
	$page = 1;
	$total = mysql_num_rows($result);
	$data = array();
	$data['page'] = $page;
	$data['total'] = $total;
	$data['rows'] = array();
	while ($row = mysql_fetch_assoc($result)) {
		$data['rows'][] = array(
			'id' => $row['id'],
			'cell' => array($row['id'], $row['id_palestra'], $row['nome_palestra'], $row['cognomenome'], $row['categoria'], $row['peso'])
		);
	}
	
	echo json_encode($data);

?>